package com.hadoop.project.gen.maodaligendata;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @tip:老师提供的HbaseDemo类 可以在这里测试数据什么的
 */
public class HBaseDemo {
    private Connection conn = null;

    // 改成你的HadoopClient路径
//    public static final String hadoopClientPath = "E:/Project/hugedata/hadoopClient";
//    public static final String ZKPath           = "192.168.21.150:2181,192.168.21.151:2181,192.168.21.152:2181";
    public static final String hadoopClientPath = "D:/hadoop";
    public static final String ZKPath           = "192.168.148.140:2181,192.168.148.141:2181,192.168.148.142:2181";

    // 连接 hbase
    //@Test
    @Before
    public void init() throws IOException {
        // 设置权限
        System.setProperty("hadoop.home.dir", hadoopClientPath);
        System.setProperty("HADOOP_USER_NAME", "root");
        // 配置连接
        Configuration config = HBaseConfiguration.create();
        config.set("hbase.zookeeper.quorum", ZKPath);
        conn = ConnectionFactory.createConnection(config);
    }

    @Test
    public void createTable() throws IOException {
        Admin admin = conn.getAdmin();
        TableName tableName = TableName.valueOf("t_user");
        TableDescriptorBuilder tUserBuilder = TableDescriptorBuilder.newBuilder(tableName);

        ColumnFamilyDescriptor tab     = ColumnFamilyDescriptorBuilder.of("tab");
        ColumnFamilyDescriptor video   = ColumnFamilyDescriptorBuilder.of("video");
        ColumnFamilyDescriptor user    = ColumnFamilyDescriptorBuilder.of("user");
        ColumnFamilyDescriptor userlog = ColumnFamilyDescriptorBuilder.of("userlog");

        tUserBuilder.addColumnFamily(tab);
        tUserBuilder.addColumnFamily(video);
        tUserBuilder.addColumnFamily(user);
        tUserBuilder.addColumnFamily(userlog);


        TableDescriptor tableInfo = tUserBuilder.build();
        admin.createTable(tableInfo); // 建好后 list 查看
        admin.close();
        conn.close();
    }

    // 插入数据
    @Test
    public void putData() throws IOException {
        Table tUser = conn.getTable(TableName.valueOf("t_user"));
        List<Put> putList = new ArrayList<>();
        Put rk001 = new Put(Bytes.toBytes("rk001"));
        rk001.addColumn(Bytes.toBytes("info"), Bytes.toBytes("name"), Bytes.toBytes("Alexmaodali")).addColumn(Bytes.toBytes("info"), Bytes.toBytes("address"), Bytes.toBytes("CQ")).addColumn(Bytes.toBytes("info"), Bytes.toBytes("age"), Bytes.toBytes("18"));
        Put rk002 = new Put(Bytes.toBytes("rk002"));
        rk001.addColumn(Bytes.toBytes("info"), Bytes.toBytes("name"), Bytes.toBytes("Alice")).addColumn(Bytes.toBytes("info"), Bytes.toBytes("address"), Bytes.toBytes("SH")).addColumn(Bytes.toBytes("info"), Bytes.toBytes("age"), Bytes.toBytes("20")).addColumn(Bytes.toBytes("data"), Bytes.toBytes("avatar"), Bytes.toBytes("avatar.png"));
        putList.add(rk001);
        putList.add(rk002);
        tUser.put(putList);
        tUser.close();
        conn.close();
    }

    // 查询
    @Test
    public void getData() throws IOException {
        Table tUser      = conn.getTable(TableName.valueOf("testGen04"));
        Get getRK001     = new Get(Bytes.toBytes("PIlsM3Xlj6"));
        Result result    = tUser.get(getRK001);
        List<Cell> cells = result.listCells();
        for (Cell cell : cells) {
            System.out.println("rowkey:" + Bytes.toString(CellUtil.cloneRow(cell)));
            System.out.println("columnFamily:" + Bytes.toString(CellUtil.cloneFamily(cell)));
            System.out.println("Qualifier:" + Bytes.toString(CellUtil.cloneQualifier(cell)));
            System.out.println("value:" + Bytes.toString(CellUtil.cloneValue(cell)));
            System.out.println("--------------------------------------------------------------------------------");
        }
        tUser.close();
        conn.close();
    }

    // 全部查询
    @Test
    public void getAllData() throws IOException {
        Table table = conn.getTable(TableName.valueOf(GenConstant.TableName));
        Scan scan = new Scan();
        ResultScanner scanner = table.getScanner(scan);
        Iterator<Result> iterator = scanner.iterator();
        while (iterator.hasNext()) {
            Result result = iterator.next();
            List<Cell> cells = result.listCells();
            for (Cell cell : cells) {
                String rowkey = Bytes.toString(CellUtil.cloneRow(cell));
                String columnFamily = Bytes.toString(CellUtil.cloneFamily(cell));
                String column = Bytes.toString(CellUtil.cloneQualifier(cell));
                String value = Bytes.toString(CellUtil.cloneValue(cell));

                System.out.println("rowkey:" + rowkey);
                System.out.println("columnFamily:" + columnFamily);
                System.out.println("Qualifier:" + column);
                System.out.println("value:" + value);
                System.out.println("--------------------------------------------------------------------------------");

                // 如果你要打印特定的列族
                // if ("tab".equals(columnFamily)) {
                //     // System.out.println("rowkey:" + Bytes.toString(CellUtil.cloneRow(cell)));
                //     // System.out.println("columnFamily:" + Bytes.toString(CellUtil.cloneFamily(cell)));
                //     // System.out.println("Qualifier:" + Bytes.toString(CellUtil.cloneQualifier(cell)));
                //     // System.out.println("value:" + Bytes.toString(CellUtil.cloneValue(cell)));
                //     if ("tid".equals(column)) System.out.println("Column:" + value);
                //     if ("tabName".equals(column)) System.out.println("TabName:" + value);
                //     // System.out.println("--------------------------------------------------------------------------------");
                // }
            }
        }
        // 关流
        table.close();
        conn.close();
    }

    // 删除
    @Test
    public void deleteData() throws IOException {
        Table tUser = conn.getTable(TableName.valueOf("t_user"));
        Delete del  = new Delete(Bytes.toBytes("rk002"));
        del.addColumn(Bytes.toBytes("info"), Bytes.toBytes("age")).addColumn(Bytes.toBytes("info"), Bytes.toBytes("address"));
        tUser.delete(del);
        tUser.close();
        conn.close();
    }

    // 删除表
    @Test
    public void dropTable() throws IOException {
        Admin admin = conn.getAdmin();
        admin.disableTable(TableName.valueOf("t_user"));
        admin.deleteTable(TableName.valueOf("t_user"));
        admin.close();
        conn.close();
    }
}
